#include <filesystem>

#include "gtest/gtest.h"

#include "DataStructure/Input/Solver/Project.h"

#include <DBManager/DBServiceFactory.h>
#include <DBManager/Database/DatabaseFactory.h>

#include "Export/NH5Export/NH5Export.h"

#include "../RandomRespSolver.h"

using namespace SG::DBManager;
namespace fs = std::filesystem;

extern std::shared_ptr<DBServiceFactory> DB_SERVICEFACTORY;

TEST (Random, calc)
{
    // 考虑到每次需要重新准备数据库，频闭测试，使用时打开
    GTEST_SKIP();

    // 读取db
    std::string dbPath = getTestDataPath ("SGFem/random/2dofspringmassmodaldampingnastran.db");
    // std::cout << "Current working directory: " << std::filesystem::current_path () << std::endl;
    fs::path    file_path (dbPath);
    std::string folderPath = file_path.parent_path ().string ();
    std::string fileName   = file_path.filename ().string ();
    // 数据库工厂
    SG::DBManager::DatabaseFactory* m_dbFactory = new SG::DBManager::DatabaseFactory ();
    // 数据库
    SG::DBManager::IDatabase* db = m_dbFactory->GetDatabase (SG::DBManager::DatabaseFactory::HDF5, folderPath, fileName);
    // 根据数据库创建Service管理器
    DB_SERVICEFACTORY = std::make_shared<SG::DBManager::DBServiceFactory> (db);

    // 调用
    SG::DataStructure::Common::IdArray ids{ 1, 2 };

    // solve
    RandomRespSolver::Solve (DB_SERVICEFACTORY, ids);

    std::string baseName = file_path.stem ().string ();          // "2dofspringmassmodaldampingnastran"
    std::string h5path   = folderPath + "/" + baseName + ".h5";  // "2dofspringmassmodaldampingnastran.h5"

    SG::DataStructure::FEM::Project project;

    project.m_jobName   = baseName;
    project.m_workSpace = ".";  // 与原始代码保持一致

    project.m_modelFilePath = folderPath;
    project.m_modelFileName = fileName;

    SGInfo << "OutPut H5 Result:" << h5path;

    SG::Export::NH5Export::WriterNastranHDF5 (DB_SERVICEFACTORY, project, h5path);
}
